package com.shouer.doc.ui.qrcode;

import android.content.Context;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.facebook.drawee.view.SimpleDraweeView;
import com.shouer.doc.R;
import com.shouer.doc.application.App;
import com.shouer.doc.common.ShouerConst;
import com.shouer.doc.common.util.FileUtil;
import com.shouer.doc.common.util.MD5Util;
import com.shouer.doc.common.util.ScreenUtils;
import com.shouer.doc.dialog.ShouerLoadingDialogUtil;
import com.shouer.doc.ui.qrcode.presenter.IQRCodePresenter;
import com.shouer.doc.ui.qrcode.presenter.QRCodePresenterImpl;
import com.shouer.doc.ui.qrcode.view.IQRCodeView;

import java.io.File;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by YYY-Work on 2017/10/23.
 */

public class QRCodeActivity extends AppCompatActivity implements IQRCodeView{

    @BindView(R.id.stuff_ico)
    protected SimpleDraweeView stuffIcoView;
    @BindView(R.id.stuff_name)
    protected TextView stuffNameView;
    @BindView(R.id.stuff_title)
    protected TextView stuffTitleView;


    @BindView(R.id.qr_code)
    protected SimpleDraweeView qrCodeImg;
    @BindView(R.id.tips)
    protected TextView tipsView;

    private App app;
    private IQRCodePresenter presenter;


    @Override
    public void onCreate(Bundle bundle)
    {
        super.onCreate(bundle);
        setContentView(R.layout.activity_qrcode_new_layout);
        ButterKnife.bind(this);
        app= (App) getApplicationContext();
        presenter=new QRCodePresenterImpl(this);
        initViews();
        presenter.queryQRCodeInfo(app.getUserDocId());

    }

    private void initViews()
    {
        if (!TextUtils.isEmpty(app.getUserDocInfo().getDocImageUrl()))
        {
            if (app.getUserDocInfo().getDocImageUrl().startsWith("http"))
            {
                stuffIcoView.setImageURI(Uri.parse(app.getUserDocInfo().getDocImageUrl()));
            }else
            {
                stuffIcoView.setImageURI(Uri.parse(FileUtil.connectFilePath(ShouerConst.RES_SERVER,app.getUserDocInfo().getDocImageUrl())));
            }
        }

        stuffNameView.setText(TextUtils.isEmpty(app.getUserDocInfo().getDocName())?"-":app.getUserDocInfo().getDocName());

        String stuffTitle=TextUtils.isEmpty(app.getUserDocInfo().getDocTitle())?"":app.getUserDocInfo().getDocTitle();
        String depart=TextUtils.isEmpty(app.getUserDocInfo().getDepartmentName())?"":app.getUserDocInfo().getDepartmentName();
        String des=stuffTitle+" "+depart;
        stuffTitleView.setText(des);
    }

    @OnClick(value = {R.id.back_btn})
    public void onClick(View view)
    {
        switch (view.getId())
        {
            case R.id.back_btn:
                finish();
                break;
        }
    }

    @Override
    public void onDestroy()
    {
        super.onDestroy();
    }

    @Override
    public Context getContext() {
        return this;
    }

    @Override
    public void createQrImageSuccess(String qrImagePath) {
        ShouerLoadingDialogUtil.getInstance().dismiss();
        Uri imgUri=Uri.fromFile(new File(qrImagePath));
        qrCodeImg.setImageURI(imgUri);
    }

    @Override
    public void createQrImageFailed(String failInfo) {
        ShouerLoadingDialogUtil.getInstance().dismiss();
        Toast.makeText(this,failInfo,Toast.LENGTH_SHORT).show();
    }

    @Override
    public void queryQrImageSuccess(String shareInfo, String tips) {

        if (!TextUtils.isEmpty(shareInfo))
        {
            String myUrlMD5= MD5Util.MD5(shareInfo);
            File appFile=getFilesDir();
            File qrFile= new File(appFile.getAbsoluteFile(),"qr/"+myUrlMD5);
            if (qrFile.exists())
            {
                Uri imgUri=Uri.fromFile(qrFile);
                qrCodeImg.setImageURI(imgUri);
                ShouerLoadingDialogUtil.getInstance().dismiss();
            }else
            {
                presenter.createQRImage(shareInfo,
                            ScreenUtils.getScreenWidth(this)-ScreenUtils.dip2px(this,100),
                            ScreenUtils.getScreenWidth(this)-ScreenUtils.dip2px(this,100),R.mipmap.ic_launcher,qrFile.getAbsolutePath());
            }

            if (!TextUtils.isEmpty(tips))
            {
                tipsView.setText(tips.replaceAll("#","\n"));
            }

        }else
        {
            ShouerLoadingDialogUtil.getInstance().dismiss();
            Toast.makeText(QRCodeActivity.this,"没有查询到相关二维码信息",Toast.LENGTH_SHORT).show();
        }


    }

    @Override
    public void queryQrImageFailed(String failInfo) {
        ShouerLoadingDialogUtil.getInstance().dismiss();
        Toast.makeText(QRCodeActivity.this,failInfo,Toast.LENGTH_SHORT).show();
    }
}
